﻿package base.titlebar
{
    import __AS3__.vec.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.utils.*;

    public class TitleBarManager extends Object
    {
        private var _showcount:int = 1;
        private var _selectfun:Function;
        private var _bob:int;
        private var _con:Sprite;
        public var mselectPos:int = 2;
        public var _index:int = 0;
        private var _actindex:int = 0;
        private var _preindex:int = 0;
        private var _items:Vector.<ITitleItem>;
        private var _maxpos:int = 1;
        private var indexArr:Array;
        public var movebob:Number = 0.2;
        private var _tiemID:uint = 0;

        public function TitleBarManager(param1:Sprite, param2:int, param3:int, param4:int = 0, param5:int = 0, param6:int = 6, param7:Function = null) : void
        {
            this._items = new Vector.<ITitleItem>;
            this._con = param1;
            this.mselectPos = param4;
            this._maxpos = param2;
            this._bob = param3;
            var _loc_8:* = param5;
            this._index = param5;
            this._actindex = _loc_8;
            this._selectfun = param7;
            this._showcount = param6;
            return;
        }// end function

        public function addItems(param1:Vector.<ITitleItem>) : void
        {
            var _loc_3:int = 0;
            var _loc_4:ITitleItem = null;
            var _loc_5:int = 0;
            this._items = param1;
            this._maxpos = this._items.length - 1;
            var _loc_2:int = 0;
            this.indexArr = [];
            while (_loc_2 <= this._maxpos)
            {
                
                this.indexArr.push(this.getIndexByPos(_loc_2));
                _loc_2++;
            }
            _loc_2 = 0;
            while (_loc_2 <= this._maxpos)
            {
                
                _loc_3 = this.indexArr[_loc_2];
                _loc_4 = this.getItem(_loc_3);
                (_loc_4 as DisplayObject).y = this._bob * _loc_2;
                _loc_5 = this.getLayer(_loc_2);
                this._con.addChild(_loc_4 as DisplayObject);
                _loc_2++;
            }
            this.getItem(this._index).selected = true;
            _loc_2 = this._showcount;
            while (_loc_2 <= this._maxpos)
            {
                
                _loc_4 = this.getItem(this.getIndexByPos(_loc_2));
                (_loc_4 as Sprite).visible = false;
                _loc_2++;
            }
            this.updatelayer();
            return;
        }// end function

        private function getIndexByPos(param1:int) : int
        {
            var _loc_2:* = param1 - this.mselectPos;
            var _loc_3:* = this._actindex + _loc_2;
            if (_loc_3 < 0)
            {
                _loc_3 = this._maxpos + _loc_3 + 1;
            }
            else if (_loc_3 > this._maxpos)
            {
                _loc_3 = _loc_3 - this._maxpos - 1;
            }
            return _loc_3;
        }// end function

        private function getItem(param1:int) : ITitleItem
        {
            return this._items[param1];
        }// end function

        public function setSelect(param1:int) : void
        {
            if (param1 == this._index || this._tiemID > 0)
            {
                return;
            }
            var _loc_2:* = this.getItem(this._index);
            _loc_2.selected = false;
            this._preindex = this._actindex;
            this._actindex = this._index;
            this._index = param1;
            var _loc_3:* = this._showcount;
            while (_loc_3 <= this._maxpos)
            {
                
                _loc_2 = this.getItem(this.getIndexByPos(_loc_3));
                (_loc_2 as Sprite).visible = true;
                _loc_3++;
            }
            this._tiemID = setInterval(this.frameHandler, this.movebob * 1000);
            return;
        }// end function

        protected function frameHandler() : void
        {
            var _loc_1:ITitleItem = null;
            var _loc_2:int = 0;
            if (this._actindex == this._index)
            {
                clearInterval(this._tiemID);
                this._tiemID = 0;
                _loc_1 = this.getItem(this._index);
                _loc_1.selected = true;
                this._selectfun(this._index);
                _loc_2 = this._showcount;
                while (_loc_2 <= this._maxpos)
                {
                    
                    _loc_1 = this.getItem(this.getIndexByPos(_loc_2));
                    (_loc_1 as Sprite).visible = false;
                    _loc_2++;
                }
            }
            else if (this._index == 0 && this._actindex == this._maxpos)
            {
                this._preindex = this._actindex;
                this._actindex = 0;
                this.updatecell(true);
            }
            else if (this._index == this._maxpos && this._actindex == 0)
            {
                this._preindex = this._actindex;
                this._actindex = this._maxpos;
                this.updatecell(false);
            }
            else if (Math.abs(this._index - this._actindex) > this._maxpos / 2)
            {
                if (this._index > this._actindex)
                {
                    if (this._actindex == 0)
                    {
                        this._preindex = this._actindex;
                        this._actindex = this._maxpos;
                    }
                    else
                    {
                        this._preindex = this._actindex;
                        var _loc_3:String = this;
                        var _loc_4:* = this._actindex - 1;
                        _loc_3._actindex = _loc_4;
                    }
                    this.updatecell(true);
                }
                else
                {
                    if (this._actindex == this._maxpos)
                    {
                        this._preindex = this._actindex;
                        this._actindex = 0;
                    }
                    else
                    {
                        this._preindex = this._actindex;
                        var _loc_3:String = this;
                        var _loc_4:* = this._actindex + 1;
                        _loc_3._actindex = _loc_4;
                    }
                    this.updatecell(false);
                }
            }
            else if (this._actindex > this._index)
            {
                this._preindex = this._actindex;
                var _loc_3:String = this;
                var _loc_4:* = this._actindex - 1;
                _loc_3._actindex = _loc_4;
                this.updatecell(false);
            }
            else
            {
                this._preindex = this._actindex;
                var _loc_3:String = this;
                var _loc_4:* = this._actindex + 1;
                _loc_3._actindex = _loc_4;
                this.updatecell(true);
            }
            return;
        }// end function

        private function updatecell(param1:Boolean) : void
        {
            var _loc_3:int = 0;
            var _loc_4:ITitleItem = null;
            var _loc_2:int = 0;
            this.indexArr.length = 0;
            this.updatelayer();
            if (param1)
            {
                _loc_2 = 0;
                while (_loc_2 <= this._maxpos)
                {
                    
                    _loc_4 = this.getItem(this.getIndexByPos(_loc_2));
                    if (_loc_2 == this._maxpos)
                    {
                        (_loc_4 as Sprite).visible = true;
                        (_loc_4 as Sprite).y = (_loc_2 + 1) * this._bob;
                    }
                    TweenLite.to(_loc_4, this.movebob, {y:_loc_2 * this._bob});
                    _loc_2++;
                }
            }
            else
            {
                _loc_2 = 0;
                while (_loc_2 <= this._maxpos)
                {
                    
                    _loc_4 = this.getItem(this.getIndexByPos(_loc_2));
                    if (_loc_2 == this._maxpos)
                    {
                        (_loc_4 as Sprite).visible = true;
                    }
                    if (_loc_2 == 0)
                    {
                        (_loc_4 as Sprite).y = -this._bob;
                    }
                    TweenLite.to(_loc_4, this.movebob, {y:_loc_2 * this._bob});
                    _loc_2++;
                }
            }
            return;
        }// end function

        private function updatelayer() : void
        {
            var _loc_2:int = 0;
            var _loc_3:ITitleItem = null;
            var _loc_4:int = 0;
            var _loc_1:int = 0;
            this.indexArr.length = 0;
            while (_loc_1 <= this._maxpos)
            {
                
                this.indexArr.push(this.getIndexByPos(_loc_1));
                _loc_1++;
            }
            _loc_1 = 0;
            while (_loc_1 <= this._maxpos)
            {
                
                _loc_2 = this.indexArr[_loc_1];
                _loc_3 = this.getItem(_loc_2);
                _loc_4 = this.getLayer(_loc_1);
                this._con.setChildIndex(_loc_3 as DisplayObject, _loc_4);
                _loc_1++;
            }
            return;
        }// end function

        private function getLayer(param1:int) : int
        {
            if (param1 < this.mselectPos)
            {
                return param1;
            }
            if (param1 == this.mselectPos)
            {
                return this._maxpos;
            }
            return this._maxpos - param1 + this.mselectPos;
        }// end function

    }
}
